<?php 
namespace Home\Controller;

use Think\Controller;

class AccountController extends CommonController{

	// 个人信息页的显示 并修改
	public function index()
	{
		$id = I('session.uid');
		$data = array('id'=>$id);
		$name = M('user')->where($data)->getField('name');
		$tel = M('user')->where($data)->getField('tel');
		$this->assign('nickname',$name);
		$this->assign('phone',$tel);
		// exit;
		// $icon = ;
		// 从数据库将头像路径分配到前台
		if (M('user')->where($data)->getField('icon')) {
			$icon = M('user')->where($data)->getField('icon'); 
			$this->assign('iconname',$icon);
		}

		// 从数据库将修改后的个人信息分配到前台
		if (M('user')->where($data)->getField('email,sex,birthday')) {
			$email = M('user')->where($data)->getfield('email');
			$sex = M('user')->where($data)->getfield('sex');
			$birthday = M('user')->where($data)->getfield('birthday');
			$this->assign('email',$email);
			$this->assign('sex',$sex);
			$this->assign('birthday',$birthday);
		}
		$this->display();
	}

	// 个人信息修改处理
	public function edit()
	{

		// dump($_POST);
		$id = I('session.uid');
		$data['name'] = I('post.username');
		$data['sex'] = I('post.sex');
		$data['email'] = I('post.email');
		$data['birthday'] = I('post.birthday');
		$res = M('user')->where(array('id'=>$id))->save($data);
		if ($res) {
			$this->success('用户修改成功');
		}else {
			$this->error();
		}
	
	}
	//对用户名的ajax验证
	public function reg()
	{	

		$username = I('post.username');
		$uname = M('user')->getField('name',true);
		if (in_array($username,$uname)) {
			echo 1;
		}else{
			echo 0;
		}
	}

	// 个人头像
	public function icon(){
		// 可以修改头像
		$id = I('session.uid');
		$data = array('id'=>$id);
		if (M('user')->where($data)->getField('icon')) {
			$icon = M('user')->where($data)->getField('icon'); 
			$this->assign('iconname',$icon);
		}
		$this->display();
	}

	// 处理个人头像
	public function doicon(){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './Uploads/'; // 设置附件上传根目录
		$upload->savePath = ''; // 设置附件上传（子）目录
		$upload->autoSub = true;//开启子目录保存
		$upload->subName = array('date','Ymd');//以日期为子目录
		// 上传文件
		$info = $upload->upload();

		// 打印上传图片的信息
		// dump($info);
		$path = $info['icon']['savepath'];
		$name = $info['icon']['savename'];
	
		if(!$info) {// 上传错误提示错误信息
		$this->error($upload->getError());
		}else{// 上传成功 获取上传文件信息
			$iname = $path.$name;
			$image = new \Think\Image();
			$image->open('./uploads/'.$iname);
			$image->thumb(100, 100)->save('./Uploads/thumb/100_'.$name);
			// var_dump('./uploads/thumb/50*50_'.$iname);die;
			$uid = I('session.uid');
			$data['icon'] = $name;
			$user = M('user');
			$res = $user->where(array('id'=>$uid))->save($data);
			// 是否添加到数据库
			if ($res > 0) {
				$this->success('上传成功！');				
			}else{
				$this->error('头像上传失败！');
			}			
		}
	}

	// 账号安全页
	public function safety()

	{
		$id = I('session.uid');
		$data = array('id'=>$id);
		if (M('user')->where($data)->getField('icon')) {
			$icon = M('user')->where($data)->getField('icon'); 
			$this->assign('iconname',$icon);
		}
		$this->display('Account/safety');
	}

	// 修改密码页
	public function password()
	{
		$this->display('Account/password');
	}

	// 执行修改密码
	public function dopass()
	{
		// dump($_POST);
		$User = D('user');
		$data['id'] = I('session.uid');
		$data['pwd'] = md5(I('post.pwd'));

		// 数据库里的密码
		$pwd = $User->where(array('id'=>I('session.uid')))->getField('pwd');
		// 表单中填写的原密码
		$oldpwd = md5(I('post.oldpwd'));
		// echo $User->fetchSql()->save($data);
		 if ($oldpwd != $pwd) {
		 	$this->error('原密码填写不正确');
		 	exit;
		 }
		 if (!$User->create()) {
		 	$this->error($User->getError());
		 	exit;
		 }

		 if ($User->save($data) > 0) {
		 	$this->success('密码修改成功');
		 } else {
		 	$this->error('密码修改失败');
		 }

	}

	// 手机换绑页面
	public function bindphone()
	{	
		$id = I('session.uid');
		$tel = M('user')->where(array('id'=>$id))->getField('tel');
		$this->assign('tel',$tel);
		$this->display();
	}

	// 发送验证码
	public function domsg()
	{	
		$tel = I('session.tel');
		$code = I('session.code');		
		$res = sendTemplateSMS("$tel",array($code,'30'),"1");
		$data = 1;
		$this->ajaxReturn($data);
	}

	// 异步生成手机验证码和得到手机号
	// 存到session中
	public function dophone()
	{
		if (!IS_POST) {
			$this->error('请返回');
		}

		$tel =  I('post.tel');
		$code = rand(1000,9999);
		session('code',$code);
		session('tel',$tel);

		$tels = M('user')->getField('tel',true);
		if (in_array($tel,$tels)) {
			echo 1;
		}else{
			echo 0;
		}
		
	} 

	// 处理手机绑定事件
	public function dobind()
	{
		if (!IS_POST) {
			$this->error('请返回');
		}

		$msg = I('post.msg');
		$tel = I('post.tel');
		$code = I('session.code');
		$id = I('session.uid');
		$User = M('user');

		$data['id'] = $id;
		$data['tel'] = $tel;
		if ($msg == $code) {
			$User->save($data);
			$this->success('手机绑定成功',U('User/outlogin'));
		}else{
			$this->error('手机绑定失败');
		}
	}


}